<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: PortalActiveRecord.php 339 2010-01-30 20:48:37Z steen.rabol@gmail.com $
 */

Prado::using('System.Data.ActiveRecord.TActiveRecord');

class PortalActiveRecord extends TActiveRecord
{
	private $_dbConn;

	public function __construct($data=array(), $connection=null)
	{
		if($connection !== null)
		{
			$this->_dbConn	= $connection;
   		}
   		else
   		{
			$app = PortalApplication::getApplication();
			$this->_dbConn	= $app->dbConnection;
		}

		parent::__construct($data,$this->_dbConn);
	}

	public function getDbConnection()
	{
		return $this->_dbConn;
	}

	public function save()
	{
		try
		{
			parent::save();
		}
		catch(Exception $e)
		{
			if($e instanceof TException)
			{
				//throw new PortalException(1,'data_error',$e->getErrorCode(),$e->getErrorMessage());
				throw new PortalException(1,sprintf("%d %s",$e->getErrorCode(),$e->getErrorMessage()));
			}
			else
			{
				//throw new PortalException(500,'data_error',$e->getCode(), $e->getMessage());
				throw new PortalException(500,sprintf("%d %s",$e->getCode(), $e->getMessage()));
			}
			
		}
	}

	public function delete()
	{
		try
		{
			parent::delete();
		}
		catch(Exception $e)
		{
			if($e instanceof TException)
			{
				throw new PortalException(1,'data_error',$e->getErrorCode(),$e->getErrormessage());
			}
			else
			{
				throw new PortalException(500, 'data_error',$e->getCode, $e->getMessage());
			}
		}
	}
}
?>